/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.business.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.sys.model.DepUser;
import com.rf.sys.model.Department;
import com.rf.sys.model.User;
import com.rf.sys.service.DepUserService;
import com.rf.sys.service.DepartmentService;
import com.rf.sys.utils.PageUtils;
import com.rf.sys.utils.ConfigUtils;
import com.rf.sys.utils.UuidUtil;
import com.rf.sys.dao.SqlAdapterMapper;
import com.rf.business.bean.NoticBean;
import com.rf.business.dao.NoticeMapper;
import com.rf.business.model.Notice;
import com.rf.business.model.NoticeDep;
import com.rf.business.model.NoticeExample;
import com.rf.business.service.NoticeDepService;
import com.rf.business.service.NoticeService;

@Service("noticeService")
public class NoticeServiceImpl implements NoticeService {
	
	@Autowired
   	private SqlAdapterMapper sqlAdapterMapper;//注入dao
   	@Autowired
   	private NoticeMapper noticeMapper;//注入dao
   
	@Resource
	private DepartmentService departmentService;

   	/**
	 * 根据id获取数据
	 * @param noticeId
	 * @return Notice
	 */
   	@Override
	public Notice getNoticeByID(String id
) throws Exception{
		// TODO Auto-generated method stub
		return noticeMapper.selectByPrimaryKey(id
);
	}
	
	/**
	 * 根据Notice获取数据
	 * @param Notice
	 * @return List<Notice>
	 */
	@Override
	public List<Notice> getNotice(Notice notice) throws Exception{
		// TODO Auto-generated method stub
		NoticeExample example = new NoticeExample();
		com.rf.business.model.NoticeExample.Criteria criteria= example.createCriteria();
		
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		List<Notice> list = noticeMapper.selectByExample(example);
		return list;
	}
	
	/**
	 * 获取所有数据
	 * @param 
	 * @return List<Notice>
	 */
	@Override
	public List<Notice> getNotice() throws Exception{
		// TODO Auto-generated method stub
		NoticeExample example = new NoticeExample();
		com.rf.business.model.NoticeExample.Criteria criteria= example.createCriteria();
		
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		return noticeMapper.selectByExample(example);
	}
   	
	/**
	 * 保存信息
	 * @param Notice
	 * @return
	 */
	@Override
	public void addNotice(Notice notice) throws Exception{
		// TODO Auto-generated method stub
		noticeMapper.insert(notice);
	}

	/**
	 * 修改数据
	 * @param Notice
	 * @param
	 */
	@Override
	public void updateNotice(Notice notice) throws Exception{
		// TODO Auto-generated method stub
		noticeMapper.updateByPrimaryKeySelective(notice);
	}
	
	/**
	 * 删除数据
	 * @param noticeId
	 * @param
	 */
	@Override
	public void deleteNotice(String id
) throws Exception{
		// TODO Auto-generated method stub
		noticeMapper.deleteByPrimaryKey(id
);
	}

	/**
	 * 获取数据分页
	 * @param Notice
	 * @param pageUtils
	 * @return Map
	 */
	@Override
	public Map getNoticePage(Notice notice,PageUtils pageUtils) throws Exception{
		NoticeExample example = new NoticeExample();
		example.setOrderByClause("CREATE_TIME DESC");
		com.rf.business.model.NoticeExample.Criteria criteria= example.createCriteria();
		Session session = SecurityUtils.getSubject().getSession();
		User user = (User)session.getAttribute("userSession");
		String depId = (String)session.getAttribute("depSessionId");
		DepUser depUser=new DepUser();
		depUser.setUserId(user.getId());
		Department dep = departmentService.getDepartmentByID(depId);
		String depName = "";
		if(dep !=null){
			depName = dep.getName();
		}
		if(notice.getTitel()!=null && !notice.getTitel().equals("")){
			criteria.andTitelLike("%"+notice.getTitel()+"%");
		}
		
		if(user.getId() != null && !user.getId().equals("")){
			criteria.andCreateUserEqualTo(user.getId());
		}
		//获取第offset页，每页共limit条内容，默认查询总数count
		Page p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getRows());
		//下一条执行语句，即为要被转换的语句
		List<Notice> list = noticeMapper.selectByExample(example);
		List<NoticBean> listBean =  new ArrayList<NoticBean>();
		if(list != null && list.size() > 0){
			for(Notice n:list){
				NoticBean nB = new NoticBean();
				nB.setId(n.getId());
				nB.setTitel(n.getTitel());
				nB.setContent(n.getContent());
				nB.setCreateTime(n.getCreateTime());
				nB.setUserName(user.getUserName());
				nB.setDepName(depName);
				listBean.add(nB);
			}
		}
		Map dateMap = new HashMap();
	    dateMap.put("total", p.getTotal());
	    dateMap.put("rows", listBean);
		return dateMap;
	}
}

